/**
 * 会员服务类
 */

const request = require('../libs/request');

class UserService {
	/**
     * 我管理的商家
	 */
	static async getBizAdmin(params = {}) {
		const { 
			showLoading = false 
		} = params;

		try {
			const response = await request.get('/v1/user/manage-bizs', {
				showLoading,
				needAuth: true,
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '获取商家列表成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取商家列表失败'
			};
		}
	}

	static async getUserInfo(params = {}) {
		const { 
			showLoading = false 
		} = params;

		try {
			const response = await request.get('/v1/user/profile', {
				showLoading,	
				needAuth: true,
			});

			return {
				success: true,
				data: response.data,	
				message: response.message || '获取用户信息成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,	
				message: error.message || '获取用户信息失败'
			};
		}
	}

	/**
	 * 设置支付密码
	 */
	static async setPayPassword(params = {}) {
		const { 
			pay_password,
			code,
			showLoading = true 
		} = params;

		try {
			const response = await request.post('/v1/user/setPayPassword', {
				pay_password,
				code
			}, {
				showLoading,
				needAuth: true,
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '设置支付密码成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '设置支付密码失败'
			};
		}
	}

	static async sendCode() {
		

		try {
			const response = await request.post('/v1/user/sendCode', {
				
			}, {
				showLoading:true,
				needAuth: true,
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '发送验证码成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '发送验证码失败'
			};
		}
	}

	static async changePassword(params = {}) {
		const { 
			password,
			code,
			showLoading = true 
		} = params;
		
		try {
			const response = await request.post('/v1/user/changePassword', {
				password,
				code
			}, {
				showLoading,
				needAuth: true,
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '修改密码成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '修改密码失败'
			};
		}
	}

	static async updateAvatar(params = {}) {
		const {
			avatar,
			showLoading = true
		} = params;
		
		try {
			const response = await request.post('/v1/user/update-avatar', {
				avatar
			}, {
				showLoading,
				needAuth: true,
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.message || '头像更新成功'
			};	
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '头像更新失败'
			};
		}
	}

	/**
	 * 更新用户信息（头像、昵称、性别、生日、单位）
	 */
	static async updateUserProfile(params = {}) {
		const {
			headimg, // 头像url
			nickname, // 昵称
			sex, // 性别 0:女 1:男
			birthday, // 生日
			company_name, // 所属单位
			company_id, // 所属单位id
			showLoading = true
		} = params;

		try {
			const response = await request.authPost('/v1/user/update-profile', {
				headimg,
				nickname,
				sex,
				birthday,
				company_name,
				company_id
			}, {
				showLoading,
				needAuth: true,
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.message || '用户信息更新成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '用户信息更新失败'
			};
		}
	}

	static async getCompanyList(params = {}) {
		const {
			showLoading = true
		} = params;

		try {
			const response = await request.get('/v1/company/index', {
				showLoading,
				needAuth: false,	
			});

			return {
				success: response.code === 1,
				data: response.data,
				message: response.message || '获取公司列表成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '获取公司列表失败'
			};
		}
	}

	/**
	 * 生成支付码记录
	 */
	static async generateQrcodeRecord(params = {}) {
		const {
			showLoading = true
		} = params;
		
		try {
			const response = await request.post('/v1/user/generate-qrcode-record', {
				showLoading,
				needAuth: true,
			});

			return {
				success: true,
				data: response.data,
				message: response.message || '生成支付码记录成功'
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '生成支付码记录失败'
			};
		}
	}

	static async checkBizUsePayQrcodeResult(params = {}) {
		const {
			showLoading = false,
			pay_qrcode_record_id
		} = params;
		
		try {
			const response = await request.post('/v1/biz-pay/check-biz-use-pay-qrcode-result', {
				pay_qrcode_record_id,
			}, {
				showLoading,	
				needAuth: false,
			});
			

			return {
				success: response.code === 1,
				data: response.data ||null,
				message: response.msg
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message
			};
		}
	}

	static async isCompanyMaster(params = {}) {
		const {
			showLoading = false
		} = params;
		
		try {
			const response = await request.get('/v1/company/is-master', {
				showLoading,
				needAuth: true,
				showErrorInfo: false,
			});

			return {
				success: response.code === 1,
				data: response.data || null,
				message: response.message || '判断是否是公司管理员成功'
			};
			
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message
			};
		}
	}

	static async updatePhone(params = {}) {
		const {
			code,
			encryptedData,
			iv
		} = params;

		try {
			const response = await request.post('/v1/user/update-phone', {
				code,
				encryptedData,
				iv
			}, {
				showLoading: false,
				needAuth: true,
			});


			return {
				success: response.code == 1,
				data: response.data || null,
				message: response.message || '更新手机号成功'
			};
		} catch (error) {
			console.log(error)
			return {
				success: false,
				data: null,
				message: error.message || '更新手机号失败'
			};
		}
	}

	static async getBalance(params = {}) {
		const {
			showLoading = false
		} = params;
		
		try {
			const response = await request.get('/v1/user/balance', {
				showLoading,
				needAuth: true,
			});

			return {
				success: response.code === 1,
				data: response.data || null,
				message: response.message
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message
			};
		}
	}

	static async isSetPayPassword(params = {}) {
		const {
			showLoading = false
		} = params;
		
		try {
			const response = await request.get('/v1/user/is-set-pay-password', {
				showLoading,
				needAuth: true,
			});
			
			return {
				success: response.code === 1,
				data: response.data || null,
				message: response.message
			};
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message
			};
		}
	}

	static async setUnInputPayPassword(params = {}) {
		const {
			showLoading = false
		} = params;
		
		try {
			const response = await request.post('/v1/user/set-un-input-pay-password', {},{
				showLoading,
				needAuth: true,
			});

			return {
				success: response.code === 1,
				data: response.data || null,
				message: response.message
			};
		} catch (error) {
			console.log(error)
			return {
				success: false,
				data: null,
				message: error.message
			};
		}
	}
}

module.exports = UserService;
